local require = require
local utils   = require("modules.business_common.business_utils")
local config  = require("modules.business_common.business_config")
local http    = require("modules.business_common.utils.business_http")

local ngx = ngx
local curr = os.time

local table = table
local pairs = pairs
local ipairs = ipairs
local tostring = tostring
local tonumber = tonumber
local loadstring = table.loadstring

local FOO = {
    _VERSION = '0.0.1'
}

local http_request = function( uin, request )
    if not (request and request.cmd) then
        return nil
    end
    request.uin  = uin
    request.time = curr()
    request.auth = ngx.md5 ( request.time .. '#miniw_907#'  .. (tostring(uin) or '') )

    local server = config.get_mall_server_url(uin)
    local response = loadstring(http.Get(server, request) or 'nil')
    if response and tonumber(response.ret) == 0 then
        return true, response.data or {}
    end

    http.Log(utils.hm.to_str({server = server, request = request, response = response}))
    return false
end

FOO.query_all_weponskins = function (uin)
    uin = uin or ngx.ctx.m_params.uin
    return http_request(uin, {
        cmd = 'query_all_weponskins'
    })
end

FOO.set_weponskin_expired_time = function (skinid, expired, uin)
    uin = uin or ngx.ctx.m_params.uin
    return http_request(uin, {
        cmd     = 'set_weponskin_optional',
        id      = skinid,
        expired = expired
    })
end

FOO.recharge_gift_callback = function(uin, gift_id, money)
    uin = uin or ngx.ctx.m_params.uin
    return http_request(uin, {
        cmd     = 'recharge_gift_callback',
        gift_id = gift_id,
        money   = money
    })
end

FOO.recharge_gift_canbuy = function(uin, gift_id)
    uin = uin or ngx.ctx.m_params.uin
    return http_request(uin, {
        cmd     = 'recharge_gift_canbuy',
        gift_id = gift_id
    })
end

return FOO